/**
 * Created by Administrator on 2017/3/5 0005.
 */
$(function(){
    function objInit(obj){
        return $(obj).html("<option>请选择</option>");
    }
    var arrData = {
        '拉格（Lager）':[
            'German-Style Pilsener',
            'Bohemian-Style Pilsener',
            'European Low-Alcohol Lager/German Leicht(bier)',
            'Münchner (Munich)-Style Helles',
            'Dortmunder/European-Style Export',
            'Vienna-Style Lager',
            'German-Style Märzen',
            'German-Style Oktoberfest/Wiesn',
            'European-Style Dark/Münchner Dunkel',
            'German-Style Schwarzbier',
            'Bamberg-Style Märzen Rauchbier',
            'Bamberg-Style Helles Rauchbier',
            'Bamberg-Style Bock Rauchbier',
            'Traditional German-Style Bock.',
            'German-Style Heller Bock/Maibock',
            'German-Style Doppelbock',
            'German-Style Eisbock',
            'Kellerbier (Cellar beer) or Zwickelbier – Lager',
            'American-Style Lager',
            'American-Style Light (Low Calorie) & Low Carbohydrate Lager',
            'American-Style Amber (Low Calorie) Lager',
            'American-Style Pilsener',
            'American-Style Ice Lager',
            'American-Style Malt Liquor',
            'American-Style Amber Lager',
            'American-Style Märzen/Oktoberfest',
            'American-Style Dark Lager',
            'Baltic-Style Porter',
            'Australasian, Latin American or Tropical-Style Light Lager'
        ],
        '小麦（Wheat）':[
            'Belgian-Style White (or Wit)/Belgian-Style Wheat',
            'South German-Style Hefeweizen/Hefeweissbier',
            'South German-Style Kristal Weizen/Kristal Weissbier',
            'German-Style Leichtes Weizen/Weissbier',
            'South German-Style Hefeweizen/Hefeweissbier',
            'South German-Style Bernsteinfarbenes Weizen/Weissbier',
            'South German-Style Dunkel Weizen/Dunkel Weissbier',
            'South German-Style Weizenbock/Weissbock',
            'Bamberg-Style Weiss (Smoke) Rauchbier (Dunkel or Helles',
            'American-Style Wheat Wine Ale',
            'American-Style Wheat Ale'
        ],
        '美式（IPA&PA）':[
            "American-Style IPA&PA"
        ],
        '非美式（IPA&PA）':[
            "British-Style IPA&PA",
            "International IPA&PA",
            "Hybrid IPA&PA"
        ],
        '淡色艾尔（Pale Ale）':[
            "Ordinary Bitter",
            "Special Bitter or Best Bitter",
            "Extra Special Bitter",
            "English-Style Summer Ale",
            "Scottish-Style Light Ale",
            "Scottish-Style Heavy Ale",
            "Scottish-Style Export Ale",
            "English-Style Pale Mild Ale",
            "Old Ale",
            "Strong Ale",
            "Scotch Ale",
            "British-Style Barley Wine Ale",
            "Fresh “Wet” Hop Ale",
            "Pale American-Belgo-Style Ale",
            "American-Style Barley Wine Ale",
            "Golden or Blonde Ale",
            "German-Style Kölsch/Köln-Style Kölsch",
            "German-Style Brown Ale/Düsseldorf-Style Altbier",
            "Kellerbier (Cellar beer) or Zwickelbier – Ale",
            "Adambier",
            "Grodzisz",
            "International-Style Pale Ale",
            "Australasian-Style Pale Ale",
            "Session Beer ",
            "American-Style Cream Ale",
            "California Common Beer",
            "Japanese Sake-Yeast Beer",
            "Rye Ale or Lager with or without Yeast",
            "German-Style Rye Ale (Roggenbier) with or without Yeast"
        ],
        '深色艾尔（Dark Ale）':[
            "Scottish-Style Heavy Ale",
            "Scottish-Style Export Ale",
            "English-Style Dark Mild Ale",
            "English-Style Brown Ale",
            "Old Ale",
            "Strong Ale",
            "Scotch Ale",
            "British-Style Barley Wine Ale",
            "Irish-Style Red Ale",
            "Fresh “Wet” Hop Ale",
            "Dark American-Belgo-Style Ale",
            "American-Style Amber/Red Ale",
            "Imperial or Double Red Ale",
            "American-Style Barley Wine Ale",
            "American-Style Brown Ale",
            "German-Style Brown Ale/Düsseldorf-Style Altbier",
            "Kellerbier (Cellar beer) or Zwickelbier – Ale",
            "Adambier",
            "Rye Ale or Lager with or without Yeast",
            "German-Style Rye Ale (Roggenbier) with or without Yeast "
        ],
        '世涛/波特 （Stout/Porter）':[
            "Brown Porter",
            "Robust Porter ",
            "Sweet Stout or Cream Stout",
            "Oatmeal Stout",
            "Classic Irish-Style Dry Stout",
            "Foreign (Export)-Style Stout",
            "Porter",
            "Smoke Porter",
            "American-Style Black Ale",
            "American-Style Stout",
            "American-Style Imperial Stout",
            "Specialty Stouts",
            "American-Style Imperial Porter",
            "Porter"
        ],
        '比利时淡色 Belgian-Style Pale Ale':[
            "Belgian-Style Flanders Oud Bruin or Oud Red Ales",
            "Belgian-Style Dubbel",
            "Belgian-Style Tripel",
            "Belgian-Style Quadrupel",
            "Belgian-Style Blonde Ale",
            "Belgian-Style Pale Ale",
            "Belgian-Style Pale Strong Ale",
            "Belgian-Style Dark Strong Ale",
            "Belgian-Style Table Beer",
            "Other Belgian-Style Ales",
            "French-Style Bière de Garde",
            "French & Belgian-Style Saison"
        ],
        '比利时深色 Belgian-Style Dark Ale':[
            "比利时深色 Belgian-Style Dark Ale",
            "Belgian-Style Dubbel",
            "Belgian-Style Tripel",
            "Belgian-Style Quadrupel",
            "Belgian-Style Pale Strong Ale",
            "Belgian-Style Dark Strong Ale",
            "French-Style Bière de Garde",
            "French & Belgian-Style Saison"
        ],
        '增味（Specialty）':[
        ],
        '试验组（Experimental）':[
        ]
    };
    //$.each(arrData,function(pF) {
    //    $("#selF").append("<option>"+pF+"</option>");
    //});
    //$("#selF").change(function() {
    //    objInit("#selT");
    //    $.each(arrData,function(pF, pS) {
    //        if ($("#selF option:selected").text() == pF) {
    //            $.each(pS,function(pT,pC) {
    //                $("#selT").append('<option value="'+this+'">'+this+'</option>');
    //            });
    //        };
    //    });
    //});


    $("form .required").each(function(){
        $(this).blur(function(){
            var $parent = $(this).siblings('h3').find('.qingtianxie');
            if($(this).hasClass('addText')){
                if(this.value==""){
                    var errorMsg = '请填写';
                    $parent.html('<span class="formtips onError">'+errorMsg+'</span>');
                }
            }
            if ($(this).hasClass('sele_opti')) {
                if(this.value==0){
                    var errorMsg = '请填写';
                    $parent.html('<span class="formtips onError">'+errorMsg+'</span>');
                }
            }
            if($(this).hasClass('apply_bie')){
                if(this.value==0){
                    var errorMsg = '请选择';
                    $parent.html('<span class="formtips onError">'+errorMsg+'</span>');
                }
            }
            if($(this).hasClass('area_text')){
                var txt = $(this).val();
                if(txt==""){
                    var errorMsg = '请填写';
                    $parent.html('<span class="formtips onError">'+errorMsg+'</span>');
                }
            }

            if ($(this).is('#phone')) {
                if(this.value==""||(this.value!=""&&!/^[0-9]{11}$/.test(this.value))){
                    var errorMsg = '请输入正确的手机格式';
                    $parent.html('<span class="formtips onError">'+errorMsg+'</span>');
                }
            }
            if ($(this).is('#province')) {
                if(this.value==0){
                    var errorMsg = '请选择';
                    $parent.html('<span class="formtips onError">'+errorMsg+'</span>');
                }
            }
        });
    })
    $("form .required").focus(function(){
        var $parent = $(this).siblings('h3');
        $parent.find(".formtips").remove();
    });


    /**
     * ajax
     * zhaoly@11space.com
     */
    var xhr;

    function createXMLHttpRequest() {
        if (window.ActiveXObject) {
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
        } else if (window.XMLHttpRequest) {
            xhr = new XMLHttpRequest();
        }
    }

    function UpladFile() {
        var fileObj = document.getElementById("photo").files[0];
        var FileController = './doajaxfileupload.php';
        var form = new FormData();
        form.append("myfile", fileObj);
        createXMLHttpRequest();
        xhr.onreadystatechange = handleStateChange;
        xhr.open("post", FileController, true);
        xhr.send(form);
    }

    function handleStateChange() {
        if (xhr.readyState == 4) {
            if (xhr.status == 200 || xhr.status == 0) {
                var result = xhr.responseText;
                var json = eval("(" + result + ")");
                $("#myphoto").val(json.file);
            }
        }
    }

    $("#photo").on("change",function(){
        UpladFile();
    })



    $("#saishi-baoming").click(function(){
        var user =   getUserInfo();
        var user_token = user.userToken;
        var match_id = getUrlParam('match_id');
        var $parent = $(this).parents('form');
        $parent.find(".formtips").remove();
        $("form .required").trigger('blur');
        var numError = $('form .onError').length;
        if(numError){
            return false;
        }
        var opus_name = $('#myform input[name="sign_name"]').val(),
            group_id = $('#selF').val(),
            style_id = $('#selT').val(),
            apply_sex = $('#baosex').val(),
            other_raw = $('#myform textarea[name="fuliao"]').val(),
            relat_data = $('#myform textarea[name="Beer_information"]').val(),
            idea = $('#myform textarea[name="idea"]').val(),
            apply_name = $('#myform input[name="alone_name"]').val(),
            apply_phone = $('#myform input[name="alone_phone"]').val(),
            apply_address = $('#province').val(),
            apply_wx = $('#myform input[name="alone_weichat"]').val();
        var data = {
            apiCode: '/Match/applyMatch',
            match_id:5,
            user_token:user_token,
            opus_name: opus_name,
            group_id:group_id,
            style_id:style_id,
            apply_sex:apply_sex,
            other_raw:other_raw,
            relat_data:relat_data,
            idea:idea,
            apply_name:apply_name,
            apply_phone:apply_phone,
            apply_address:apply_address,
            apply_wx:apply_wx,
        };
        http(data, function(responed){
            console.log(responed);
            if( responed.success == 1 ){
                $('.qurentijiao').show();
                setTimeout(function(){
                    location.href = 'canjia-m.html?match_id=5';
                }, 1000);
            }else{
                mui.alert(responed.msg);
            }
        });
    });
    var pro = ["北京", "上海", "天津", "重庆", "浙江", "江苏", "广东", "福建", "湖南", "湖北", "辽宁",
        "吉林", "黑龙江", "河北", "河南", "山东", "陕西", "甘肃", "新疆", "青海", "山西", "四川",
        "贵州", "安徽", "江西", "云南", "内蒙古", "西藏", "广西", "宁夏", "海南", "香港", "澳门", "台湾"];
    for(var i=0;i<pro.length;i++){
        $option=$("<option/>");
        $option.attr("value",pro[i]);
        $option.text(pro[i]);
        $("#province").append($option);
    }


})








